//
//  AdvertisingPageViewCommentCell.swift
//  iModern
//
//  Created by Ralf on 7/27/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit

class AdvertisingPageViewCommentCell: TableViewCell
{
    let __comment_label_tags       = 50
    let __comment_image_tags       = 51
    let __Avatar_label_tags        = 52
    let __button_tag                  = 53
    let __all_button_tag           = 54
    var ida = Int()
    
    override func awakeFromNib()
    {
        super.awakeFromNib()
        // Initialization code
        
        initCommentLabel()
        initAvatarImageView()
        initAvatarLabel()
        initButton()
        initAllButton()
    }
    
    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    func setData(data: NSDictionary, advertisingID: Int)
    {
        self.ida = advertisingID
        //let advertising = data["advertising"] as! [String:AnyObject]
        let assignedTo  = data["assignedTo"] as! [String:AnyObject]
        //let contact     = data["contact"] as! [String:AnyObject]
        
        let score       = data["score"] as! Int
        let review      = data["review"] as! String
        
        let label2 = self.viewWithTag(__Avatar_label_tags) as? UILabel
        label2?.text =  assignedTo["shopName"] as? String
        
        let label = self.viewWithTag(__comment_label_tags) as? UILabel
        label!.numberOfLines=3
        label!.adjustsFontSizeToFitWidth = true
        label?.text = review
        
        let imageView = self.viewWithTag(__comment_image_tags) as? UIImageView
        let thumbnailURL = assignedTo["thumbnailURL"] as? String
        if thumbnailURL == nil
        {
            
        }
        else
        {
            let url = appDelegate.parameterManager.server + "/" + thumbnailURL!
            imageView!.load(url, placeholder: nil)
        }
        
        let button = self.viewWithTag(__button_tag) as? UILabel
        if score == 0
        {
            button!.backgroundColor = UIColor(red: 245 / 225, green: 154 / 225, blue: 47 / 225, alpha: 1)
            button?.text = "一般"
        }
        if score == 1
        {
            button?.text = "不错"
            button!.backgroundColor = UIColor(red: 122 / 225, green: 179 / 225, blue: 61 / 225, alpha: 1)
        }
        if score == 2
        {
            button?.text = "超赞"
            button!.backgroundColor = UIColor(red: 215 / 225, green: 51 / 225, blue: 155 / 225, alpha: 1)
        }
        if score == 3
        {
            button?.text = "神作"
            button!.backgroundColor = UIColor(red: 215 / 225, green: 51 / 225, blue: 155 / 225, alpha: 1)
        }
        if score == -1
        {
            button?.text = "没评"
            button!.backgroundColor = UIColor(red: 224 / 225, green: 30 / 225, blue: 30 / 225, alpha: 1)
        }
    }
    
    func initCommentLabel() //160
    {
        let screen = UIScreen.mainScreen().bounds
        var label = self.viewWithTag(__comment_label_tags) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 10, y: 10, width: screen.width - 20, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(12)
            label!.tag = __comment_label_tags
            label?.text = "包装用心，礼物好精美，东西非常非常棒。膜拜中"
            label?.textColor = UIColor.blackColor()
            self.addSubview(label!)
        }
    }
    
    func initAvatarImageView()
    {
        var imageView = self.viewWithTag(__comment_image_tags) as? UIImageView
        if (imageView == nil)
        {
            let frame = CGRect(x: 10.0, y:60.0, width: 40.0, height: 40.0)
            imageView = UIImageView(frame: frame)
            imageView!.image = UIImage(named: "Avatar-Default.png")
            imageView!.tag = __comment_image_tags
            imageView!.layer.cornerRadius = 20.0
            imageView!.layer.masksToBounds = true
            imageView!.layer.borderColor = UIColor.blackColor().CGColor
            imageView!.layer.borderWidth = 1.0
            self.addSubview(imageView!)
        }
        
    }
    
    func initAvatarLabel() //160
    {
        var label = self.viewWithTag(__Avatar_label_tags) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 80, y: 60, width: 100, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(14)
            label!.tag = __Avatar_label_tags
            label?.text = "包装用心"
            label?.textColor = UIColor.blackColor()
            self.addSubview(label!)
        }
    }
    
    func initButton()
    {
        let screen = UIScreen.mainScreen().bounds
        var button = self.viewWithTag(__button_tag) as? UILabel
        if (button == nil)
        {
            let frame = CGRect(x: screen.width - 55, y: 70, width: 40, height: 20)
            button = UILabel(frame: frame)
            button!.font = UIFont.systemFontOfSize(14)
            button?.textColor = UIColor.whiteColor()
            button!.tag = __button_tag
            button!.layer.borderWidth = 1.0
            button!.layer.borderColor = UIColor.whiteColor().CGColor
            button!.layer.masksToBounds = true
            button!.layer.cornerRadius = 8
            button?.textAlignment = NSTextAlignment.Center
            self.addSubview(button!)
        }
    }
    
    func initAllButton()
    {
        let screen = UIScreen.mainScreen().bounds
        var button = self.viewWithTag(__all_button_tag) as? UIButton
        if (button == nil)
        {
            let frame = CGRect(x: (screen.width - 120) / 2, y: 120, width: 120, height: 30)
            button = UIButton(frame: frame)
            button!.setTitle("全部评价", forState: .Normal)
            button!.titleLabel?.font = UIFont.systemFontOfSize(14)
            button!.setTitleColor(UIColor.whiteColor(), forState: .Normal)
            button!.backgroundColor = UIColor(red: 0.40784313725490196, green: 0.6588235294117647, blue: 0.19215686274509805, alpha: 1)
            button!.layer.masksToBounds = true
            button!.layer.cornerRadius = 4
            button!.addTarget(self, action: Selector("all"), forControlEvents: UIControlEvents.TouchUpInside)
            button!.tag = __all_button_tag
            self.addSubview(button!)
        }
    }
    
    func all()
    {
        let controller = AllCommentsViewController()
        controller.id = ida
        appDelegate.navigationController?.pushViewController(controller, animated: true)
    }
    
}
